<?php

namespace App\Cache;

use App\Constants\RecommendedGradeCode;
use App\Helpers\DateUtil;
use App\Helpers\RedisUtil;

class UserScoreCache
{
//    public static function buildKey($array){
//        $script = <<<lua
//            local key = redis.call('exists',KEYS[1])
//            if key == 1 then
//                return 0
//            else
//                redis.call('sAdd',KEYS[1],0)
//                redis.call('Expire',KEYS[1],ARGV[1])
//            end
//        lua;
//
//        $redis = RedisUtil::getRedis();
//
//        foreach ($array as $item) {
//            $redis->eval($script,[
//                $item,
//                DateUtil::getTodayEnds() + 120
//            ],1);
//        }
//    }
    /**
     * 添加等级用户
     * @param $key
     * @param $userId
     * @return bool|int
     */
    public static function addGradeUser($key,$userId)
    {
        return RedisUtil::getRedis()->sAdd($key, $userId);
    }

    /**
     * 删除等级用户
     * @param $key
     * @param $userId
     * @return int
     */
    public static function delGradeUser($key,$userId): int
    {
        return RedisUtil::getRedis()->sRem($key, $userId);
    }

    /**
     * 获取角色
     * @param $key
     * @return array
     */
    public static function getGradeUser($key): array
    {
        return RedisUtil::getRedis()->sMembers($key);
    }

    /**
     * 删除所有等级
     * @return int
     */
    public static function delAllScore(): int
    {
        $redis = RedisUtil::getRedis();

        return $redis->del(
            RedisKey::todayUserGradeList(RecommendedGradeCode::MALE_TOURIST_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::MALE_DOWNSTREAM_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::MALE_MIDSTREAM_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::MALE_UPSTREAM_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::FEMALE_NEWCOMER_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::FEMALE_POTENTIAL_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::FEMALE_RECOMMEND_CODE),
            RedisKey::todayUserGradeList(RecommendedGradeCode::FEMALE_OPTIMIZE_CODE),
        );
    }
}